//
//  ShowPicturesCell.m
//  QA
//
//  Created by xiaowu on 2017/5/22.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "ShowPicturesCell.h"

static CGFloat commonOffset = 10;
static NSInteger baseTag = 129019;

@interface ShowPicturesCell ()

@end

@implementation ShowPicturesCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


+ (CGFloat)getHeightWithArr:(NSArray *)imageUrls {
    CGFloat height = 0;
    CGFloat itemWidth = (ScreenWidth - 4 * commonOffset) / 3.0;
    if (imageUrls.count == 1) {
        // 1张
#warning 图片宽高比
        height += 2 * itemWidth + commonOffset;
    } else if ((imageUrls.count > 1) && (imageUrls.count <= 9)){
        // 2张以上
        height += (((imageUrls.count - 1) / 3) + 1) * (itemWidth + commonOffset);
    } else {
//        height += (9 / 3) * (itemWidth + commonOffset);
    }
    
    return height;
}

+ (CGSize)calculateOriginal:(CGSize)original size:(CGSize)itemSize {
    CGSize size = CGSizeZero;
    
    if (original.width > original.height) {
        // 横 图
        CGFloat rate = original.width / original.height;
        if (original.width > itemSize.width) {
            // 大于最大尺寸
            if ((itemSize.width / rate) < 0.5 * itemSize.width) {
                // 高 小于 最大宽的0.5
                size.height = 0.5 * itemSize.width;
            } else {
                size.height= itemSize.width / rate;
            }
            size.width = itemSize.width;
        } else {
            // 原始比例 (没有计算最小值)
            size = original;
        }
    } else {
        // 竖 图
        CGFloat rate = original.width / original.height;
        if (original.height > itemSize.height) {
            // 大于 最大高
            if (rate * itemSize.height < 0.5 * itemSize.height) {
                size.width = 0.5 * itemSize.height;
            } else {
                size.width = rate * itemSize.height;
            }
            size.height = itemSize.height;
        } else {
        // 原始比例(没有计算最小值)
            size = original;
        }
    }
    return size;
}

- (void)setImageShowStyleWithArr:(NSMutableArray *)imageUrls {
    [self.contentView removeAllSubviews];
    CGFloat itemWidth = (ScreenWidth - 4 * commonOffset) / 3.0;

    if (imageUrls.count == 1) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(commonOffset, 0,  2 * itemWidth, 2 * itemWidth);
        imageView.backgroundColor = [UIColor imageEDBackgroundColor];
        imageView.userInteractionEnabled = YES;
        imageView.tag = baseTag;
        
        [imageView setContentMode:UIViewContentModeCenter];
        imageView.clipsToBounds = YES;
        [imageView sd_setImageWithURL:[NSURL URLWithString:[imageUrls firstObject]] placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];

        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [imageView addGestureRecognizer:tapGesture];
        
        [self.contentView addSubview:imageView];
    } else {
    
        for (NSInteger i = 0; i < imageUrls.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
        
            if (imageUrls.count == 4 && i > 1) {
                // 特殊,4个 展示 2x2
                imageView.frame = CGRectMake(commonOffset * ((i % 2 + 1)) + ((i + 1) % 3) * itemWidth, 0 + ((i + 1) / 3) * (itemWidth + commonOffset), itemWidth, itemWidth);
            } else {
                imageView.frame = CGRectMake(commonOffset * (i % 3 + 1) + (i % 3) * itemWidth, 0 + (i / 3) * (itemWidth + commonOffset), itemWidth, itemWidth);
            }
            imageView.backgroundColor = [UIColor imageEDBackgroundColor];
            imageView.userInteractionEnabled = YES;
            imageView.tag = baseTag + i;
            [imageView setContentMode:UIViewContentModeCenter];
            imageView.clipsToBounds = YES;
            [imageView sd_setImageWithURL:[NSURL URLWithString:[imageUrls objectAtIndex:i]] placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
            
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
            [imageView addGestureRecognizer:tapGesture];
            
            [self.contentView addSubview:imageView];
        }
    }
}

- (void)tapGesture:(UITapGestureRecognizer *)gesture {
    
    if (self.block) {
        self.block((UIImageView *)gesture.view, gesture.view.tag - baseTag);
    }
    
}


@end
